from fastapi import FastAPI, Body, HTTPException
import logging
from article_manager import fetch as fetch_article
from model import Input
from init import init_app

from fastapi.responses import FileResponse
from tts import text_to_speech

app = FastAPI()

init_app(app)

@app.get("/")
async def index():
    return "Working"

@app.post("/fetch_article")
async def fetch(input:Input):
    result = fetch_article(input.url)
    return result

@app.post('/generate_audio')
def tts(text:str = Body(...)):
    logging.info(f"tts: {text}")
    try:
        audio_path = text_to_speech(text)
        return FileResponse(audio_path, media_type="audio/mpeg", filename="output.mp3")
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))